package hutech.ksv.view;

import hutech.ksv.common.KenhSinhVienContants;
import hutech.ksv.entity.TaiKhoanCNTENtity;
import hutech.ksv.entity.TaiKhoanCQAEntity;
import hutech.ksv.entity.TaiKhoanCTEntity;
import hutech.ksv.entity.TaiKhoanSVEntity;
import hutech.ksv.entity.UserAccountHashtable;
import hutech.ksv.service.EncryptMD5;
import hutech.ksv.service.UserAccountService;

import java.io.IOException;
import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("session")
public class UserLoginManagedBean implements Serializable {
	/**
	 * 
	 */	
	
	@Autowired
	UserAccountService userAccountService;
	
	private TaiKhoanSVEntity taiKhoanSV;
	private TaiKhoanCNTENtity taiKhoanCNT;
	private TaiKhoanCQAEntity taiKhoanCQA;
	private TaiKhoanCTEntity taiKhoanCT;
	private int loaiTaiKhoan;
	private int loaiNhanXet;
	
	
	public TaiKhoanSVEntity getTaiKhoanSV() {
		return taiKhoanSV;
	}

	public void setTaiKhoanSV(TaiKhoanSVEntity taiKhoanSV) {
		this.taiKhoanSV = taiKhoanSV;
	}

	public TaiKhoanCNTENtity getTaiKhoanCNT() {
		return taiKhoanCNT;
	}

	public void setTaiKhoanCNT(TaiKhoanCNTENtity taiKhoanCNT) {
		this.taiKhoanCNT = taiKhoanCNT;
	}

	public TaiKhoanCQAEntity getTaiKhoanCQA() {
		return taiKhoanCQA;
	}

	public void setTaiKhoanCQA(TaiKhoanCQAEntity taiKhoanCQA) {
		this.taiKhoanCQA = taiKhoanCQA;
	}

	public TaiKhoanCTEntity getTaiKhoanCT() {
		return taiKhoanCT;
	}

	public void setTaiKhoanCT(TaiKhoanCTEntity taiKhoanCT) {
		this.taiKhoanCT = taiKhoanCT;
	}

	public int getLoaiTaiKhoan() {
		return loaiTaiKhoan;
	}

	public void setLoaiTaiKhoan(int loaiTaiKhoan) {
		this.loaiTaiKhoan = loaiTaiKhoan;
	}

	public int getLoaiNhanXet() {
		return loaiNhanXet;
	}

	public void setLoaiNhanXet(int loaiNhanXet) {
		this.loaiNhanXet = loaiNhanXet;
	}

	//On load
	public void onLoad() {
		if(userAccountHashtable != null) {
			try {
				FacesContext.getCurrentInstance().getExternalContext().redirect("welcome.xhtml");
			} catch (IOException e) {
				e.printStackTrace();
				System.out.println(e.getMessage());
			}
		}
	}
	
	public String login() {		
		userAccountHashtable = userAccountService.checkLogin(username, password);
		if(userAccountHashtable != null) {
			if(userAccountHashtable.getKey() == KenhSinhVienContants.TAI_KHOAN_SINH_VIEN) {
				taiKhoanSV = new TaiKhoanSVEntity();
				taiKhoanSV = (TaiKhoanSVEntity) userAccountHashtable.getValue();
				loaiTaiKhoan = KenhSinhVienContants.TAI_KHOAN_SINH_VIEN;
				return "welcome";
			}
			if(userAccountHashtable.getKey() == KenhSinhVienContants.TAI_KHOAN_CHU_QUAN_AN) {
				taiKhoanCQA = new TaiKhoanCQAEntity();
				taiKhoanCQA = (TaiKhoanCQAEntity) userAccountHashtable.getValue();
				loaiTaiKhoan = KenhSinhVienContants.TAI_KHOAN_CHU_QUAN_AN;
				return "welcome";
			}
			if(userAccountHashtable.getKey() == KenhSinhVienContants.TAI_KHOAN_CONG_TY) {
				taiKhoanCT = new TaiKhoanCTEntity();
				taiKhoanCT = (TaiKhoanCTEntity) userAccountHashtable.getValue();
				loaiTaiKhoan = KenhSinhVienContants.TAI_KHOAN_CONG_TY;
				return "welcome";
			}
			if(userAccountHashtable.getKey() == KenhSinhVienContants.TAI_KHOAN_CHU_NHA_TRO) {
				taiKhoanCNT = new TaiKhoanCNTENtity();
				taiKhoanCNT = (TaiKhoanCNTENtity) userAccountHashtable.getValue();
				loaiTaiKhoan = KenhSinhVienContants.TAI_KHOAN_CHU_NHA_TRO;
				return "welcome";
			}
		}
		return "dangNhapLoi";
	}	
	
	public String logout() {
		userAccountHashtable = null;
		return "UserLogin";
	}
	
	
	//Khai bao cac propeties
	private static final long serialVersionUID = 1L;
	
	private String username;
	private String password;
	UserAccountHashtable userAccountHashtable;
	
	public UserAccountHashtable getUserAccountHashtable() {
		return userAccountHashtable;
	}
	public void setUserAccountHashtable(UserAccountHashtable userAccountHashtable) {
		this.userAccountHashtable = userAccountHashtable;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}
